package com.token;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Value("${jwt.sercetKey}")
    private String jwt_sercetKey;

    @Value("${jwt.keeptime}")
    private long  jwt_keeptime;

    @Autowired
    private LoginRealm loginRealm;

    @Bean
    public SecurityManager securityManager(){
        JwtUtil.sercetKey=jwt_sercetKey;
        JwtUtil.keeptime = jwt_keeptime;
        DefaultSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        DefaultSubjectDAO defaultSubjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSubjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        defaultSecurityManager.setSubjectDAO(defaultSubjectDAO);
        defaultSecurityManager.setRealm(loginRealm);
        return  defaultSecurityManager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map filterMap = new HashMap();
        shiroFilterFactoryBean.setFilters(filterMap);
        Map<String,String> fiterRealmMap = new HashMap<>();
        filterMap.put("jwt",new JWTFilter());
        fiterRealmMap.put("/","anon");
        fiterRealmMap.put("/admin/station/query/StationLogin","anon");
        fiterRealmMap.put("/Stationlogin.html","anon");
        fiterRealmMap.put("/admin/viewstyle/*","anon");
        fiterRealmMap.put("/admin/viewstyle/*/*","anon");
        fiterRealmMap.put("/admin/viewstyle/*/*/*","anon");
        fiterRealmMap.put("/admin/*.html","anon");
        fiterRealmMap.put("/admin/*/*.html","anon");
        fiterRealmMap.put("/admin/*/*/*.html","anon");
        fiterRealmMap.put("/admin/login","anon");
        fiterRealmMap.put("/admin/theme/selWeishenheCount","anon");
        fiterRealmMap.put("/admin/css/*","anon");
        fiterRealmMap.put("/admin/js/*","anon");
        fiterRealmMap.put("/favicon.ico","anon");
        fiterRealmMap.put("/images/**","anon");
        fiterRealmMap.put("/css/**","anon");
        fiterRealmMap.put("/js/**","anon");
        fiterRealmMap.put("/lib/**","anon");
        fiterRealmMap.put("/src/**","anon");
        fiterRealmMap.put("/img/**","anon");
        fiterRealmMap.put("/admin/terminal-key/selectTerminalKeysExcel","anon");
        fiterRealmMap.put("/admin/terminal/excelTerminalList","anon");
        fiterRealmMap.put("/upload/**","anon");
        fiterRealmMap.put("/screen/**","anon");
        fiterRealmMap.put("/js/layui/css/modules/layer/default/icon.png","anon");
        fiterRealmMap.put("/admin/station_config/**","anon");
        fiterRealmMap.put("/ztree/*/*","anon");
        fiterRealmMap.put("/ztree/*/**","anon");
        fiterRealmMap.put("/swagger*/**","anon");
        fiterRealmMap.put("/v2/api*/*","anon");
        fiterRealmMap.put("/**","jwt,authc");
        shiroFilterFactoryBean.setSuccessUrl("/admin/main.html");
        shiroFilterFactoryBean.setLoginUrl("/index.html");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(fiterRealmMap);
        return shiroFilterFactoryBean;
    }
}
